package org.gpp.proj1.robot.part.parser;

import java.io.FileNotFoundException;

import org.gpp.proj1.bone.parser.BoneStructureParserException;
import org.gpp.proj1.robot.part.Part;
import org.gpp.proj1.robot.part.PartType;
import org.gpp.proj1.util.test.JMETestCase;

import com.jme.math.Quaternion;

/**
 * Tests for the file part parser class
 *  
 * @author Paolo Victor, paolovictor@gmail.com
 */
public class PartFileParserTest extends JMETestCase  {
	public void testParseGoodFile() throws BoneStructureParserException {
		Part part = null;
		try {
			part = PartFileParser.parseFile( "test/part/test.part" );
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			fail("The file exists");			
		} catch (InvalidPartFileException e) {
			e.printStackTrace();
			fail("The file is valid");
		}
		
		assertNotNull( "Part file exists and is valid", part );
		assertEquals( "Arm of Awesomeness", part.getName() );
		assertEquals( 200000, part.getPrice() );
		assertEquals( PartType.ARM, part.getPartType() );
		assertNotNull( "Model exists", part.getModel( "arm1" ) );
		assertNotNull( "Model exists", part.getModel( "forearm1" ) );
		assertNotNull( "Model exists", part.getModel( "arm2" ) );
		assertNotNull( "Model exists", part.getModel( "forearm2" ) );
		assertTrue( part.getModelNames().contains( "arm1" ) );
		assertTrue( part.getModelNames().contains( "forearm1" ) );
		assertTrue( part.getModelNames().contains( "arm2" ) );
		assertTrue( part.getModelNames().contains( "forearm2" ) );
		assertNotNull( part.getEmissor( "cannon") );
		assertNotNull( part.getEmissor( "burstCannon" ) );
		assertNotNull( part.getEmissor( "cannon" ).getEmissorNode( "arm1" ) );
		assertNotNull( part.getEmissor( "cannon" ).getEmissorNode( "arm2" ) );
		assertNotNull( part.getEmissor( "burstCannon" ).getEmissorNode( "arm1" ) );
		assertNotNull( part.getEmissor( "burstCannon" ).getEmissorNode( "arm2" ) );
		assertEquals( 1.0f, part.getEmissor( "cannon" ).getEmissorNode( "arm1" ).getLocalTranslation().x );
		assertEquals( 2.0f, part.getEmissor( "cannon" ).getEmissorNode( "arm1" ).getLocalTranslation().y );
		assertEquals( 3.0f, part.getEmissor( "cannon" ).getEmissorNode( "arm1" ).getLocalTranslation().z );
		assertEquals( 4.0f, part.getEmissor( "cannon" ).getEmissorNode( "arm2" ).getLocalTranslation().x );
		assertEquals( 5.0f, part.getEmissor( "cannon" ).getEmissorNode( "arm2" ).getLocalTranslation().y );
		assertEquals( 6.0f, part.getEmissor( "cannon" ).getEmissorNode( "arm2" ).getLocalTranslation().z );
		assertEquals( 7.0f, part.getEmissor( "burstCannon" ).getEmissorNode( "arm1" ).getLocalTranslation().x );
		assertEquals( 8.0f, part.getEmissor( "burstCannon" ).getEmissorNode( "arm1" ).getLocalTranslation().y );
		assertEquals( 9.0f, part.getEmissor( "burstCannon" ).getEmissorNode( "arm1" ).getLocalTranslation().z );
		assertEquals( 10.0f, part.getEmissor( "burstCannon" ).getEmissorNode( "arm2" ).getLocalTranslation().x );
		assertEquals( 11.0f, part.getEmissor( "burstCannon" ).getEmissorNode( "arm2" ).getLocalTranslation().y );
		assertEquals( 12.0f, part.getEmissor( "burstCannon" ).getEmissorNode( "arm2" ).getLocalTranslation().z );
		
		Quaternion quat = new Quaternion();
		quat.fromAngles( 1.5f, 2.5f, 3.5f );		
		assertEquals( quat, part.getEmissor( "cannon" ).getEmissorNode( "arm1" ).getLocalRotation() );
		quat.fromAngles( 4.5f, 5.5f, 6.5f );
		assertEquals( quat, part.getEmissor( "cannon" ).getEmissorNode( "arm2" ).getLocalRotation() );
		quat.fromAngles( 7.5f, 8.5f, 9.5f );		
		assertEquals( quat, part.getEmissor( "burstCannon" ).getEmissorNode( "arm1" ).getLocalRotation() );
		quat.fromAngles( 10.5f, 11.5f, 12.5f );
		assertEquals( quat, part.getEmissor( "burstCannon" ).getEmissorNode( "arm2" ).getLocalRotation() );		
	}
	
	public void testParseBadFile() throws BoneStructureParserException {
		try {
			PartFileParser.parseFile( "test/part/testNOTEXISTS.part" );
			fail();
		} catch (FileNotFoundException e) {
		} catch (InvalidPartFileException e) {
			e.printStackTrace();
			fail( "The file does not exist" );
		}
		
		try {
			PartFileParser.parseFile( "test/part/testInvalid.part" );
			fail();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			fail( "The file exists" );
		} catch (InvalidPartFileException e) {
			assertEquals( "part.price", e.getInvalidField() );
		}
	}
}
